package jjn.round1;

/**
 * @author Jjn
 * @since 2022/8/28 18:58
 */
public class LeetCode724_FindPivotIndex {

    public int pivotIndex(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            int totalLeft = calcTotalFromLeft(nums, i);
            int totalRight = calcTotalFromRight(nums, i);
            if (totalLeft == totalRight) {
                return i;
            }
        }
        return -1;
    }

    private int calcTotalFromLeft(int[] nums, int index) {
        int total = 0;
        for (int i = 0; i < index; i++) {
            total += nums[i];
        }
        return total;
    }

    private int calcTotalFromRight(int[] nums, int index) {
        int total = 0;
        for (int i = index + 1; i < nums.length; i++) {
            total += nums[i];
        }
        return total;
    }
}
